<?php

class TencentModel extends SCModel {
	
	public function __construct() {
		parent::__construct();
	}
	
	
	/**
	 * 
	 * 保存QQ用户基本信息
	 * @param array $data QQ用户基本信息
	 * @return boolean
	 */
	public function saveUserInfo(array $data) {
		if($this->from('sc_tencent')->where('open_id = "'.$data['open_id'].'"')->findOne()) {
			$this->reset()->from('sc_tencent')
				->where('open_id = "'.$data['open_id'].'"')
				->update(array(
					'access_token' => $data['access_token'],
					'nickname' => $data['nickname'],
					'figureurl' => $data['figureurl'],
					'figureurl_1' => $data['figureurl_1'],
					'figureurl_2' => $data['figureurl_2'],
					'gender' => $data['gender']
				)
			);
		} else {
			$this->reset()->from('sc_tencent')->insert(array(
				'open_id' => $data['open_id'],
				'access_token' => $data['access_token'],
				'nickname' => $data['nickname'],
				'figureurl' => $data['figureurl'],
				'figureurl_1' => $data['figureurl_1'],
				'figureurl_2' => $data['figureurl_2'],
				'gender' => $data['gender']
			));
		}
		return true;
	}
	
	/**
	 * 检查指定OpenId是否已与本网站用户绑定
	 * 检查OpenId对应的记录user_id是否为0即可
	 * @param string $open_id QQ用户OpenId
	 * @return boolean
	 */
	public function isBind($open_id) {
		return $this->getBindUserId($open_id);
	}
	
	/**
	 * 获取OpenId所绑定的用户ID
	 * @param string $open_id
	 * @return integer
	 */
	public function getBindUserId($open_id) {
		return $this->reset()->field('user_id')->from('sc_tencent')->where('open_id = "'.$open_id.'"')->findScalar();
	}
	
} 